# 代码 3-23 有序列表
class Node:
    def __init__(self, date):
        self.next = None
        self.date = date

    def getNext(self):
        return self.next

    def setNext(self, newNext):
        self.next = newNext

    def getDate(self):
        return self.date

    def setDate(self, newDate):
        self.date = newDate


class OrderedList:
    def __init__(self):
        self.head = None;

    def add(self, item):
        current = self.head
        previous = None
        stop = False
        while current != None and not stop:
            if current.getDate() > item:
                stop = True
            else:
                previous = current
                current = current.getNext()

        temp = Node(item)
        if previous == None:
            temp.setNext(self.head)
            self.head = temp
        else:
            temp.setNext(current)
            previous.setNext(temp)

    def remove(self, item):
        current = self.head
        previous = None
        found = False
        while current != None and not found:
            if current.getDate() == item:
                found = True
            else:
                previous = current
                current = current.getNext()
        if previous == None:
            self.head = current.getNext()
        else:
            previous.setNext(current.getNext())

    def search(self, item):
        current = self.head
        found = False
        stop = False
        while current != None and not found and not stop:
            if current.getDate() == item:
                found = True
            else:
                if current.getDate() > item:
                    stop = True
                else:
                    current = current.getNext()
        return found

    def isEmpty(self):
        return self.head == None

    def length(self):
        current = self.head
        count = 0
        while current != None:
            count += 1
            current = current.getNext()
        return count


o = OrderedList()
o.add(17)
o.add(26)
o.add(54)
o.add(77)
o.add(93)
print(o.length())
print(o.search(31))
o.add(31)
print(o.length())
